<?php
/**
 * Created by PhpStorm.
 * User: 小夏
 * Date: 2016/10/11 0011
 * Time: 13:37
 */
namespace Payment\Controller;
use Common\Controller\HomebaseController;

class DemoController extends HomebaseController{
    protected $course;
    protected $activity;
    protected $ordergoods;
    protected $orderinfo;
    function _initialize() {
        parent::_initialize();
        $this->course = M("Course");
        $this->activity = M("Activity");
        $this->ordergoods = M("OrderGoods");
        $this->orderinfo = M("OrderInfo");
    }
    public function wx_notity_callback($data='',$msg=''){

        if(defined('__FROM_WXPAY_LIB__')){
            //在这里实现你的逻辑
            parse_str($data['attach']);//把查询字符串解析到变量中
            $result = $this->orderinfo->where(array('order_sn'=>$ordersn))->find();
            if($result){
                $value = array('pay_time'=>time(),'confirm_time'=>time(),'pay_status'=>1,'order_status'=>1,'out_trade_no'=>$data['out_trade_no']);
                $this->orderinfo->where(array('order_sn'=>$ordersn))->setField($value);
            }else{
                $this->log('wx_new_loc.txt',var_export($ordersn,true)."—————订单不存在———————\r\n");
            }
        }else{
            $this->log('wx_new_log.txt',var_export($data,true),$msg."支付失败———FAILED———\r\n");
        }

        return true;
    }

    /*
     * 微信支付异步处理
     */
    public function pay_return()
    {
        vendor("Wxpay.Notify");
        $notify = new \WxPayNotifyCallBack();
        $notify->setCallback(array($this,'wx_notity_callback'));
        $notify->Handle(false);
        exit;
    }

    /**日志生成
     * @param $file
     * @param $txt
     */
    function log($file,$txt)
    {
        $fp =  fopen($file,'ab+');
        fwrite($fp,'-----------'.date('Y-m-d H:i:s').'-----------------');
        fwrite($fp,$txt);
        fwrite($fp,"\r\n\r\n\r\n");
        fclose($fp);
    }
}
